package handler

import (
	"comment-video/api/request"
	"comment-video/global"
	"comment-video/internal/dao"
	"comment-video/internal/model"
	"context"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

func AddComment(c *gin.Context) {
	var req request.AddCommentForm
	var comment model.Comment

	token := c.GetHeader("x-token")

	if err := c.ShouldBind(&req); err != nil {
		return
	}

	if req.VideoId == 0 || req.UserId == 0 || req.Info == "" {
		c.JSON(http.StatusOK, gin.H{
			"code": 100000,
			"msg":  "验证失败",
			"data": nil,
		})
		return
	}

	get := global.RDB.Get(context.Background(), fmt.Sprintf("token:%d", token)).Val()

	if get != strconv.Itoa(int(comment.UserId)) {
		c.JSON(http.StatusOK, gin.H{
			"code": 100000,
			"msg":  "权限不足",
			"data": false,
		})
		return
	}

	comment = model.Comment{
		VideoId:       req.VideoId,
		UserId:        req.UserId,
		CommentId:     req.CommentId,
		LoadCommentId: req.LoadCommentId,
		Info:          req.Info,
	}

	err := dao.AddFile(&comment)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 100000,
			"msg":  "评论失败",
			"data": false,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "评论成功",
		"data": true,
	})

}

func DeletedComment(c *gin.Context) {
	var req request.DeletedCommentForm
	var comment model.Comment

	token := c.GetHeader("x-token")

	if err := c.ShouldBind(&req); err != nil {
		return
	}

	if req.Id == 0 || req.UserId == 0 || req.VideoId == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 100000,
			"msg":  "验证失败",
			"data": nil,
		})
		return
	}

	get := global.RDB.Get(context.Background(), fmt.Sprintf("token:%d", token)).Val()
	if get != strconv.Itoa(int(req.UserId)) {
		c.JSON(http.StatusOK, gin.H{
			"code": 100000,
			"msg":  "权限不足",
			"data": nil,
		})
	}

	comment = model.Comment{
		Id:      req.Id,
		UserId:  req.UserId,
		VideoId: req.VideoId,
	}

	err := dao.DeletedFile(&comment)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 100000,
			"msg":  "删除失败",
			"data": false,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "删除成功",
		"data": true,
	})

}

func ShowComment(c *gin.Context) {

}

/*
func ShowByAuthComment(c *gin.Context) {

}*/

func FindByAuthComment(c *gin.Context) {

}

func ListComment(c *gin.Context) {

}
